
package com.enjoyiot.module.bms.sevice.device;

import com.enjoyiot.framework.common.pojo.PageResult;
import com.enjoyiot.framework.common.util.object.BeanUtils;
import com.enjoyiot.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.enjoyiot.module.bms.controller.admin.device.vo.BmsDevice;
import com.enjoyiot.module.bms.controller.admin.device.vo.BmsDevicePageReqVO;
import com.enjoyiot.module.bms.controller.admin.device.vo.BmsDeviceSaveReqVO;
import com.enjoyiot.module.bms.dal.dataobject.AssetDO;
import com.enjoyiot.module.bms.dal.dataobject.BmsDeviceDO;
import com.enjoyiot.module.bms.dal.mysql.asset.AssetMapper;
import com.enjoyiot.module.bms.dal.mysql.device.BmsDeviceMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static com.enjoyiot.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.enjoyiot.module.bms.enums.ErrorCodeConstants.ASSET_NOT_EXISTS;

@Service
@Validated
public class BmsDeviceServiceImpl implements BmsDeviceService {

    @Resource
    private BmsDeviceMapper bmsDeviceMapper;

    @Resource
    private AssetMapper assetMapper;

    @Override
    public Long createDevice(BmsDeviceSaveReqVO createReqVO) {
        // 插入
        BmsDeviceDO bmsDevice = BeanUtils.toBean(createReqVO, BmsDeviceDO.class);
        bmsDeviceMapper.insert(bmsDevice);
        // 更新资产类型为：设备
        AssetDO updateObj = new AssetDO();
        updateObj.setId(bmsDevice.getAssetId());
        updateObj.setType(1);
        assetMapper.updateById(updateObj);
        // 返回
        return bmsDevice.getId();
    }

    @Override
    public void updateDevice(BmsDeviceSaveReqVO updateReqVO) {
        // 校验存在
        validateBmsDeviceExists(updateReqVO.getId());
        // 更新
        BmsDeviceDO updateObj = BeanUtils.toBean(updateReqVO, BmsDeviceDO.class);
        bmsDeviceMapper.updateById(updateObj);
    }

    @Override
    public void deleteDevice(Long id) {
        // 校验存在
        BmsDeviceDO device = bmsDeviceMapper.selectById(id);
        if (device == null) {
            throw exception(ASSET_NOT_EXISTS);
        }
        // 删除
        bmsDeviceMapper.deleteById(id);
        // 更新资产类型为：一般资产
        AssetDO updateObj = new AssetDO();
        updateObj.setId(device.getAssetId());
        updateObj.setType(0);
        assetMapper.updateById(updateObj);
    }

    private void validateBmsDeviceExists(Long id) {
        if (bmsDeviceMapper.selectById(id) == null) {
            throw exception(ASSET_NOT_EXISTS);
        }
    }

    @Override
    public BmsDeviceDO getDevice(Long id) {
        return bmsDeviceMapper.selectById(id);
    }

    @Override
    public PageResult<BmsDevice> getDevicePage(BmsDevicePageReqVO pageReqVO) {
        return bmsDeviceMapper.selectPage(pageReqVO);
    }

    @Override
    public void updateDeviceStatus(Long id, Integer status) {
        // 校验用户存在
        validateBmsDeviceExists(id);
        // 更新状态
        BmsDeviceDO updateObj = new BmsDeviceDO();
        updateObj.setId(id);
        updateObj.setDeviceStatus(status);
        bmsDeviceMapper.updateById(updateObj);
    }

    @Override
    public Integer syncAsset() {
        //查询所有bms_device表中的asset_id
        List<Long> ids = bmsDeviceMapper.selectList().stream().map(BmsDeviceDO::getAssetId).collect(Collectors.toList());
        if(ids.size() > 0) {
            //查询bms_device的asset_id中type=1但是not in ids的所有id
            List<Long> notExistAssetIds = assetMapper.selectNotExistAssetIds(ids);
            if(notExistAssetIds.size() > 0) {
                notExistAssetIds.forEach(assetId -> {
                    // 更新资产类型为：设备
                    BmsDeviceDO updateObj = new BmsDeviceDO();
                    updateObj.setAssetId(assetId);
                    updateObj.setDeviceStatus(0);
                    updateObj.setAcceptanceStatus(0);
                    bmsDeviceMapper.insert(updateObj);
                });
            }
            return notExistAssetIds.size();
        }
        return 0;
    }

}
